package com.guoranhao.business.api.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.guoranhao.business.api.service.BaseInfoApi;
import com.guoranhao.business.model.*;
import com.guoranhao.business.param.AdminGetAppFunctionIconListParam;
import com.guoranhao.business.param.AppFunctionIconParam;
import com.guoranhao.business.param.EsSkuParam;
import com.guoranhao.business.service.*;
import com.guoranhao.business.vo.AppFunctionIconAdminVo;
import com.guoranhao.business.vo.EsSpuVo;
import com.guoranhao.tool.constants.BaseErrorCodeEnum;
import com.guoranhao.tool.enums.ZeroOrOneEnum;
import com.guoranhao.tool.model.BasePage;
import com.guoranhao.tool.utils.LogUtil;
import com.guoranhao.tool.utils.StringUtil;
import com.guoranhao.tool.vo.AdminResultVo;
import com.guoranhao.tool.vo.MessageVo;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Description:  基础接口实现<br/>
 * Version V1.0  2019-8-30 16:58  by 秦涛   (qintao@cloud-young.com) 创建
 */
@Component
@Service(interfaceClass = BaseInfoApi.class, version = "1.0.1")
public class BaseInfoApiImpl implements BaseInfoApi {

    private static final Logger logger = LoggerFactory.getLogger(BaseInfoApiImpl.class);

    @Autowired
    private AppFunctionIconService appFunctionIconService;

    @Autowired
    private SlideConfigService slideConfigService;

    @Autowired
    private PostService postService;

    @Autowired
    private EsCategoryService esCategoryService;

    @Autowired
    private EsSpuService esSpuService;


    @Override
    public MessageVo<List<AppFunctionIcon>> getAppFunctionIconList(Long categoryId) {
        try {
            if (Objects.isNull(categoryId)) {
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(), BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            AppFunctionIconExample example = new AppFunctionIconExample();
            example.createCriteria().andAppFunctionIconCategoryIdEqualTo(categoryId);
            example.setOrderByClause("ord desc");
            return new MessageVo<>(true,appFunctionIconService.selectByExample(example));
        } catch (Exception e) {
            LogUtil.error(logger, e, "getAppFunctionIconList_method_error,categoryId:{0}", categoryId);
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100009.getCode(), BaseErrorCodeEnum.ERRCODE_100009.getMsg());
    }

    @Override
    public MessageVo<AdminResultVo<AppFunctionIconAdminVo>> getAppFunctionIconListForAdmin(AdminGetAppFunctionIconListParam param) {

        return new MessageVo<>(true, appFunctionIconService.getAppFunctionIconListForAdmin(param));
    }

    @Override
    public MessageVo<Boolean> modifyAppFunctionIconListForAdmin(Long userId, Long appFunctionIconId, String functionIconUrl) {
        return null;
    }

    @Override
    public MessageVo<Boolean> modifyAppFunctionIcon(AppFunctionIconParam param) {
        return null;
    }

    @Override
    public MessageVo<Boolean> addNewFunctionIcon(AppFunctionIconParam param) {
        return null;
    }

    @Override
    public MessageVo<List<SlideConfig>> getSlideConfig(BasePage page) {
        try {
            if (Objects.isNull(page)) {
                page.setPageNo(ZeroOrOneEnum.ONE.getCode());
                page.setPageSize(10);
            }
            SlideConfigExample example = new SlideConfigExample();
            SlideConfigExample.Criteria criteria = example.createCriteria();
            criteria.andSlideStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
            example.setOffset(page.getPageStart());
            example.setLimit(page.getPageSize());
            example.setOrderByClause("show_weight desc");
            return new MessageVo<>(true, slideConfigService.selectByExample(example));
        } catch (Exception e) {
            LogUtil.error(logger, e, "getSlideConfig_method_error,page:{0}", JSON.toJSONString(page));
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100008.getCode(), BaseErrorCodeEnum.ERRCODE_100008.getMsg());
    }

    @Override
    public MessageVo<List<Post>> getPostList(BasePage page) {
        try {
            if (Objects.isNull(page)){
                page.setPageNo(ZeroOrOneEnum.ONE.getCode());
                page.setPageSize(10);
            }
            PostExample example = new PostExample();
            PostExample.Criteria criteria = example.createCriteria();
            criteria.andStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
            criteria.andAuditStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
            example.setOrderByClause("recommend desc,post_id desc");
            return new MessageVo<>(true,postService.selectByExample(example));
        }catch (Exception e){
            LogUtil.error(logger,e,"getPostList_method_error,page:{0}", JSON.toJSONString(page));
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100010.getCode(),BaseErrorCodeEnum.ERRCODE_100010.getMsg());
    }

    @Override
    public MessageVo<List<EsCategory>> getEsCategoryList() {
        EsCategoryExample example = new EsCategoryExample();
        EsCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
        example.setOrderByClause("category_id desc");
        return new MessageVo<>(true,esCategoryService.selectByExample(example));
    }

    @Override
    public MessageVo<Integer> getShoppingCartCount(String openId) {
        try {
            if (StringUtil.isEmpty(openId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return new MessageVo<>(true,esSpuService.getShoppingCartCount(openId));
        }catch (Exception e){
            LogUtil.error(logger,e,"getShoppingCartCount_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100012.getCode(),BaseErrorCodeEnum.ERRCODE_100012.getMsg());
    }

    @Override
    public MessageVo<List<EsSpuVo>> getShoppingCartList(String openId) {
        try {
            if (StringUtil.isEmpty(openId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return new MessageVo<>(true,esSpuService.getShoppingCartList(openId));
        }catch (Exception e){
            LogUtil.error(logger,e,"getShoppingCartList_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100013.getCode(),BaseErrorCodeEnum.ERRCODE_100013.getMsg());
    }

    @Override
    public MessageVo<Boolean> addGoods(ShoppingCart shoppingCart) {
        try {
            if (Objects.isNull(shoppingCart) || Objects.isNull(shoppingCart.getSkuId()) || StringUtil.isEmpty(shoppingCart.getOpenId()) || Objects.isNull(shoppingCart.getQuantity())){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            return new MessageVo<>(true,esSpuService.addGoods(shoppingCart));
        }catch (Exception e){
            LogUtil.error(logger,e,"addGoods_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100014.getCode(),BaseErrorCodeEnum.ERRCODE_100014.getMsg());
    }

    @Override
    public MessageVo<List<EsSpuVo>> getEsSkuList(EsSkuParam param) {
        try {
            if (Objects.isNull(param) || Objects.isNull(param.getCategoryId())){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            EsSpuExample example = new EsSpuExample();
            EsSpuExample.Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(param.getCategoryId());
            criteria.andSpuStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
            criteria.andPublishStatusEqualTo(ZeroOrOneEnum.ONE.getCode());
            example.setOrderByClause("spu_id desc");
            example.setOffset(param.getPageStart());
            example.setLimit(param.getPageSize());

            List<EsSpu> esSpus = esSpuService.selectByExample(example);
            return new MessageVo<>(true,esSpus.stream().map(EsSpuVo::toVo).collect(Collectors.toList()));
        }catch (Exception e){
            LogUtil.error(logger,e,"getEsSkuList_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100015.getCode(),BaseErrorCodeEnum.ERRCODE_100015.getMsg());
    }

    @Override
    public MessageVo<EsSpuVo> getEsSkuvoByEsSkuId(Long esSkuId) {
        try {
            if (Objects.isNull(esSkuId)){
                return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100004.getCode(),BaseErrorCodeEnum.ERRCODE_100004.getMsg());
            }
            EsSpuExample example = new EsSpuExample();
            EsSpuExample.Criteria criteria = example.createCriteria();
            criteria.andSpuIdEqualTo(esSkuId);
            List<EsSpu> esSpus = esSpuService.selectByExample(example);
            if (CollectionUtils.isNotEmpty(esSpus)){
                EsSpuVo vo = new EsSpuVo();
                BeanUtils.copyProperties(esSpus.get(0),vo);
                return new MessageVo<>(true,vo);
            }
            return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100016.getCode(),BaseErrorCodeEnum.ERRCODE_100016.getMsg());
        }catch (Exception e){
            LogUtil.error(logger,e,"getEsSkuvoByEsSkuId_method_error");
        }
        return new MessageVo<>(false, BaseErrorCodeEnum.ERRCODE_100016.getCode(),BaseErrorCodeEnum.ERRCODE_100016.getMsg());
    }
}